from option import Option
import signals
import stats
import gtk

f_state = False
s_state = False

def on_backend_prepare(sig, dat, usr):
	global f_state, s_state
	
	s_state = f_state
	f_state = (Option().session.options().dump(), Option().mode.options().dump())

def cancel(widget, num):
	global f_state, s_state

	if not s_state:
		dial = gtk.MessageDialog(message_format = 'There\'s no sufficient data. Isn\'t your session empty, actually?', buttons = gtk.BUTTONS_OK)
		dial.run()
		dial.hide()
		return

	Option().session.options().dump(s_state[0])
	Option().mode.options().dump(s_state[1])

	Option().session.repopulate_scores()
	Option().windowMain.refresh()

def tidyDB(lower,upper, mode_only = False):
	cur = stats.CONN.cursor()

	mode = 'AND mode_id = ' + str(stats.getModeId()) if mode_only else ''

	num = cur.execute(' '.join(['SELECT count(*) FROM scores WHERE score >=',str(lower),'AND score <',str(upper),str(mode)])).fetchall()[0][0]

	cur.execute(' '.join(['DELETE FROM scores WHERE score >=',str(lower),'AND score <',str(upper),str(mode)]))

	dial = gtk.MessageDialog(message_format = 'Done. Deleted %i scores.' % num if num != 1 else 'Done. Deleted one score.', buttons = gtk.BUTTONS_OK)
	dial.run()
	dial.hide()

def tidyDB_custom(widget):
	dial = gtk.Dialog('Custom DB cleaner', Option().windowMain.window, gtk.DIALOG_MODAL)

	lower = gtk.Entry()
	upper = gtk.Entry()

	dial.vbox.set_border_width(12)
	dial.vbox.set_spacing(6)

	table = gtk.Table(4,2)
	table.set_row_spacings(6)
	table.set_col_spacings(6)

	table.attach(gtk.Label('Delete scores that are'),0,2,0,1)

	table.attach(gtk.Label('Equal or greater than:'),0,1,1,2)
	table.attach(lower,1,2,1,2)

	table.attach(gtk.Label('but'),0,3,2,3)

	table.attach(gtk.Label('Lower than:'),0,1,3,4)
	table.attach(upper,1,2,3,4)
	dial.vbox.pack_start(table)
	mode_only = gtk.CheckButton(_('Process only current mode'))

	dial.vbox.pack_start(mode_only)

	dial.add_button(gtk.STOCK_OK , gtk.RESPONSE_OK)
	dial.add_button(gtk.STOCK_CANCEL , gtk.RESPONSE_CANCEL)

	dial.show_all()
	resp = dial.run()
	dial.hide()

	if resp == gtk.RESPONSE_OK:
		tidyDB(float(lower.get_text()), float(upper.get_text()), mode_only.get_active() )

def tidyDB_generic(widget):
	dial = gtk.MessageDialog(message_format = 'This utility will evaporate all saved sub-1sec values. Are you sure?', buttons = gtk.BUTTONS_YES_NO)
	response = dial.run()
	dial.hide()

	if response == gtk.RESPONSE_YES:
		tidyDB(0,1)

def init():
	menu = gtk.Menu()

	item = gtk.MenuItem(label = _('Cancel last score'))
	item.connect('activate',cancel,1)
	item.show()
	menu.append(item)

	item = gtk.MenuItem(label = _('Tidy DB'))
	item.connect('activate',tidyDB_generic)
	item.show()
	menu.append(item)

	item = gtk.MenuItem(label = _('Tidy DB (customized)'))
	item.connect('activate',tidyDB_custom)
	item.show()
	menu.append(item)

	menu.show()
	toolmenu = gtk.MenuItem(label = 'Utilities')
	toolmenu.set_submenu(menu)
	toolmenu.show()

	Option().windowMain.menuTools.append(toolmenu)

	signals.connect('backend.prepare',on_backend_prepare)

def info():
	return {'name':'mod_utils',
			'author':'Michal Hordecki',
			'description':'Handy utilities for Cube Assistant.',
			}

def teardown():
	pass

